package com.xjrsoft.module.customerTwo.AppManage.service_support_order_service.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.dev33.satoken.annotation.SaMode;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.xjrsoft.common.page.PageOutput;
import com.xjrsoft.common.page.ConventPage;

import java.util.ArrayList;
import java.util.List;
import com.xjrsoft.common.utils.DataTransUtil;
import com.xjrsoft.common.result.Response;
import com.xjrsoft.core.tool.utils.BeanUtil;
import com.xjrsoft.module.base.service.IXjrBaseCodeRuleService;
import com.xjrsoft.module.customerTwo.AppManage.sales_order.entity.SalesOrder;
import com.xjrsoft.module.customerTwo.AppManage.sales_order.service.ISalesOrderService;
import com.xjrsoft.module.customerTwo.AppManage.sales_order.vo.SalesOrderVo;
import com.xjrsoft.module.customerTwo.AppManage.service_support_order_service.dto.ServiceSupportOrderDto;
import com.xjrsoft.module.customerTwo.AppManage.service_support_order_service.dto.SaveServiceSupportOrderFormDataDto;
import com.xjrsoft.module.customerTwo.AppManage.service_support_order_service.vo.ServiceSupportOrderFormDataVo;
import com.xjrsoft.module.customerTwo.AppManage.service_support_order_service.vo.ServiceSupportOrderListVo;
import com.xjrsoft.module.customerTwo.AppManage.service_support_order_service.dto.ServiceSupportOrderListDto;
import com.xjrsoft.module.customerTwo.AppManage.service_support_order_service.vo.ServiceSupportOrderTableVo;
import com.xjrsoft.module.customerTwo.AppManage.service_support_order_service.entity.ServiceSupportOrderTable;
import com.xjrsoft.module.customerTwo.AppManage.service_support_order_service.dto.ServiceSupportOrderTableDto;
import com.xjrsoft.module.customerTwo.AppManage.service_support_order_service.vo.ServiceSupportOrderTableDetailsVo;
import com.xjrsoft.module.customerTwo.AppManage.service_support_order_service.entity.ServiceSupportOrderTableDetails;
import com.xjrsoft.module.customerTwo.AppManage.service_support_order_service.dto.ServiceSupportOrderTableDetailsDto;
import com.xjrsoft.module.customerTwo.AppManage.service_support_order_service.vo.ServiceSupportServiceVo;
import com.xjrsoft.module.customerTwo.AppManage.service_support_order_service.entity.ServiceSupportService;
import com.xjrsoft.module.customerTwo.AppManage.service_support_order_service.dto.ServiceSupportServiceDto;
import com.xjrsoft.core.secure.utils.SecureUtil;
import com.xjrsoft.core.tool.utils.FlowableUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import io.swagger.models.auth.In;
import lombok.AllArgsConstructor;

import org.springframework.web.bind.annotation.*;
import com.xjrsoft.module.customerTwo.AppManage.service_support_order_service.entity.ServiceSupportOrder;
import com.xjrsoft.module.customerTwo.AppManage.service_support_order_service.vo.ServiceSupportOrderVo;
import com.xjrsoft.module.customerTwo.AppManage.service_support_order_service.service.IServiceSupportOrderService;

/**
 *  控制器
 *
 * @author lisen
 * @since 2024-04-26
 */
@RestController
@AllArgsConstructor
@RequestMapping("/service_support_order_service")
@Api(value = "售后工单", tags = "售后工单接口")
public class ServiceSupportOrderController {


	private final IServiceSupportOrderService serviceSupportOrderService;

	private final ISalesOrderService salesOrderService;

	private final IXjrBaseCodeRuleService codeRuleService;

	/**
	 * 详情 
	 */
	@GetMapping("/{iid}")
	@ApiOperation(value = "详情", notes = "传入serviceSupportOrder")
	@SaCheckPermission(value = {"service_support_order_service:view", "service_support_order_service:modify"}, mode = SaMode.OR)
	public Response<ServiceSupportOrderFormDataVo> getFormData(@PathVariable String iid) {
		Integer id = Integer.parseInt(iid);
		ServiceSupportOrderFormDataVo formDataVo = new ServiceSupportOrderFormDataVo();
		// 主表数据
		ServiceSupportOrder serviceSupportOrder = serviceSupportOrderService.getById(id);
		ServiceSupportOrderVo serviceSupportOrderVo = BeanUtil.copy(serviceSupportOrder, ServiceSupportOrderVo.class);
		SalesOrder salesOrder = salesOrderService.getById(serviceSupportOrder.getOrderId());
		SalesOrderVo salesOrderVo = BeanUtil.copy(salesOrder, SalesOrderVo.class);
		serviceSupportOrderVo.setSalesOrderVo(salesOrderVo);

		formDataVo.setServiceSupportOrderVo(serviceSupportOrderVo);
		// 从表数据
		List<ServiceSupportOrderTable> serviceSupportOrderTableByParentId = serviceSupportOrderService.getServiceSupportOrderTableByParentId(id);
		List<ServiceSupportOrderTableVo> serviceSupportOrderTableVoList = BeanUtil.copyList(serviceSupportOrderTableByParentId, ServiceSupportOrderTableVo.class);
		formDataVo.setServiceSupportOrderTableVoList(serviceSupportOrderTableVoList);

		List<ServiceSupportOrderTableDetailsVo> serviceSupportOrderTableDetailsVoList = BeanUtil.copyList(serviceSupportOrderService.getServiceSupportOrderTableDetailsByParentId(id), ServiceSupportOrderTableDetailsVo.class);
		formDataVo.setServiceSupportOrderTableDetailsVoList(serviceSupportOrderTableDetailsVoList);

		List<ServiceSupportServiceVo> serviceSupportServiceVoList = BeanUtil.copyList(serviceSupportOrderService.getServiceSupportServiceByParentId(id), ServiceSupportServiceVo.class);
		formDataVo.setServiceSupportServiceVoList(serviceSupportServiceVoList);

		return Response.ok(formDataVo);
	}

	/**
	 * 自定义分页 
	 */
	@GetMapping
	@ApiOperation(value = "分页", notes = "传入serviceSupportOrder")
		public Response<PageOutput<ServiceSupportOrderListVo>> getPageList(ServiceSupportOrderListDto listDto) {
		IPage<ServiceSupportOrder> page = serviceSupportOrderService.getPageList(listDto);
		List<ServiceSupportOrderListVo> records = BeanUtil.copyList(page.getRecords(), ServiceSupportOrderListVo.class);
			// 转换列表数据
		DataTransUtil.transListShowData(records);
			// 工作流数据处理
		FlowableUtil.systemFormList(records, SecureUtil.getModuleId());
		return Response.ok(ConventPage.getPageOutput(page.getTotal(), records));
	}

	/**
	 * 客户新增工单维护
	 */
	@PostMapping
	@ApiOperation(value = "新增", notes = "传入SaveServiceSupportOrderFormDataDto")
	@SaCheckPermission("service_support_order_service:add")
	public Response save(@RequestBody SaveServiceSupportOrderFormDataDto formDto) {
		ServiceSupportOrderDto serviceSupportOrderDto = formDto.getServiceSupportOrderDto();
		ServiceSupportOrder serviceSupportOrder = BeanUtil.copy(serviceSupportOrderDto, ServiceSupportOrder.class);

		ServiceSupportOrderTableDto serviceSupportOrderTableDto = formDto.getServiceSupportOrderTableDto().get(0);
		ServiceSupportOrderTable serviceSupportOrderTab = BeanUtil.copy(serviceSupportOrderTableDto, ServiceSupportOrderTable.class);

		List<ServiceSupportOrderTableDetailsDto> serviceSupportOrderTableDetailsDtoList = serviceSupportOrderTableDto.getServiceSupportOrderTableDetailsDto();
		List<ServiceSupportOrderTableDetails> serviceSupportOrderTableDetailsList = BeanUtil.copyList(serviceSupportOrderTableDetailsDtoList, ServiceSupportOrderTableDetails.class);

		Integer dataId = serviceSupportOrderService.addServiceSupportOrder(serviceSupportOrder, serviceSupportOrderTab, serviceSupportOrderTableDetailsList);
		if (dataId != null) {
			codeRuleService.useEncode("ServiceSupportOrder");
		}
		return dataId == null ? Response.notOk() : Response.ok(dataId);
	}

	/**
	 * 服务商提供维护方案
	 */
	@PostMapping("/service/{iid}")
	@ApiOperation(value = "维护", notes = "传入id,SaveServiceSupportOrderFormDataDto")
	@SaCheckPermission("service_support_order_service:service")
	public Response addService(@PathVariable String iid, @RequestBody SaveServiceSupportOrderFormDataDto formDto) {
		Integer id = Integer.parseInt(iid);
		ServiceSupportServiceDto serviceSupportServiceDto = formDto.getServiceSupportServiceDto().get(0);
		ServiceSupportService serviceSupportService = BeanUtil.copy(serviceSupportServiceDto, ServiceSupportService.class);
		return Response.status(serviceSupportOrderService.addServiceSupportservice(id,serviceSupportService));
	}

	/**
	 * 客户评价
	 */
	@PostMapping("/opinion")
	@ApiOperation(value = "评价", notes = "传入SaveServiceSupportOrderFormDataDto")
	@SaCheckPermission("service_support_order_service:opinion")
	public Response opinion(@RequestBody SaveServiceSupportOrderFormDataDto formDto) {
		ServiceSupportOrderDto serviceSupportOrderDto = formDto.getServiceSupportOrderDto();
		ServiceSupportOrder serviceSupportOrder = BeanUtil.copy(serviceSupportOrderDto, ServiceSupportOrder.class);
		return Response.status(serviceSupportOrderService.opinion(serviceSupportOrder));
	}

	/**
	 * 修改 
	 */
	@PutMapping("/order/{iid}")
	@ApiOperation(value = "修改", notes = "传入id,SaveServiceSupportOrderFormDataDto")
	@SaCheckPermission("service_support_order_service:update")
	public Response updateOrder(@PathVariable String iid, @RequestBody SaveServiceSupportOrderFormDataDto formDto) {
		Integer id = Integer.parseInt(iid);
		ServiceSupportOrderDto serviceSupportOrderDto = formDto.getServiceSupportOrderDto();
		ServiceSupportOrder serviceSupportOrder = BeanUtil.copy(serviceSupportOrderDto, ServiceSupportOrder.class);

		List<ServiceSupportOrderTableDto> serviceSupportOrderTableDtos = formDto.getServiceSupportOrderTableDto();
		List<ServiceSupportOrderTable> serviceSupportOrderTables = new ArrayList<>();

		for ( ServiceSupportOrderTableDto i:
			 serviceSupportOrderTableDtos) {
			ServiceSupportOrderTable  orderTable = BeanUtil.copy(i, ServiceSupportOrderTable.class);
			List<ServiceSupportOrderTableDetailsDto> serviceSupportOrderTableDetailsDtos = i.getServiceSupportOrderTableDetailsDto();
			List<ServiceSupportOrderTableDetails> serviceSupportOrderTableDetailsList = BeanUtil.copyList(serviceSupportOrderTableDetailsDtos, ServiceSupportOrderTableDetails.class);
			orderTable.setServiceSupportOrderTableDetailsList(serviceSupportOrderTableDetailsList);
			serviceSupportOrderTables.add(orderTable);
		}
		return Response.status(serviceSupportOrderService.updateServiceSupportOrder(serviceSupportOrder,serviceSupportOrderTables));
	}
	/**
	 * 删除 
	 */
	@DeleteMapping("/{ids}")
	@ApiOperation(value = "删除", notes = "传入ids")
	@SaCheckPermission(value = {"service_support_order_service:batchDelete", "service_support_order_service:delete"}, mode = SaMode.OR)
	public Response remove(@ApiParam(value = "主键集合", required = true) @PathVariable String ids) {
		return Response.status(serviceSupportOrderService.delete(ids));
	}

}
